package com.wiz.weixin.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wiz.weixin.common.Constant;
import com.wiz.weixin.criteria.WxPayCriteria;
import com.wiz.weixin.manager.HttpManager;
import com.wiz.weixin.service.PayService;
import com.wiz.weixin.to.PayOrderTO;

@Service("payService")
public class PayServiceImpl implements PayService {
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private Gson gson;
	@Resource
	private HttpManager httpManager;

	@Override
	public PayOrderTO getWxUnifiled(String hqId, String stationId, WxPayCriteria criteria) throws Exception {
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
		formparams.add(new BasicNameValuePair("hqId", hqId));
		formparams.add(new BasicNameValuePair("stationId", stationId));
		formparams.add(new BasicNameValuePair("order", gson.toJson(criteria)));
		formparams.add(new BasicNameValuePair("payChannel", String.valueOf(4)));
    	String response = httpManager.getResponseByHttpPost(Constant.PAY_WEB_URL, formparams);
    	log.info("stationId="+stationId+" getWxUnifiled,response = "+response);
    	if(StringUtils.isBlank(response)){
    		return null;
    	}
    	PayOrderTO to = gson.fromJson(response, new TypeToken<PayOrderTO>(){}.getType());
		return to;
	}

	@Override
	public void closeWebOrder(String hqId, String stationId, String payOrderId) throws Exception {
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
		formparams.add(new BasicNameValuePair("hqId", hqId));
		formparams.add(new BasicNameValuePair("stationId", stationId));
		formparams.add(new BasicNameValuePair("payOrderId", payOrderId));
		formparams.add(new BasicNameValuePair("payChannel", String.valueOf(4)));
    	httpManager.getResponseByHttpPost(Constant.CANCEL_WEB_URL, formparams);
	}

}
